<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="../../../../lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.useso.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="../../../../lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="../../../../css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="../../../../images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="../../../../images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="../../../../images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="../../../../images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="redis-cluster,">










<meta name="description" content="Redis 集群简介Redis 是一个开源的 key-value 存储系统，由于出众的性能，大部分互联网企业都用来做服务器端缓存。Redis 在3.0版本前只支持单实例模式，虽然支持主从模式、哨兵模式部署来解决单点故障，但是现在互联网企业动辄大几百G的数据，可完全是没法满足业务的需求，所以，Redis 在 3.0 版本以后就推出了集群模式。 Redis 从3.0.0正式版开始官方支持集群, Red">
<meta name="keywords" content="redis-cluster">
<meta property="og:type" content="article">
<meta property="og:title" content="Redis集群详细搭建指南">
<meta property="og:url" content="http://demo_demo.gitee.io/2017/06/04/redis-ji-qun-xiang-xi-da-jian-zhi-nan/index.html">
<meta property="og:site_name" content="运维人">
<meta property="og:description" content="Redis 集群简介Redis 是一个开源的 key-value 存储系统，由于出众的性能，大部分互联网企业都用来做服务器端缓存。Redis 在3.0版本前只支持单实例模式，虽然支持主从模式、哨兵模式部署来解决单点故障，但是现在互联网企业动辄大几百G的数据，可完全是没法满足业务的需求，所以，Redis 在 3.0 版本以后就推出了集群模式。 Redis 从3.0.0正式版开始官方支持集群, Red">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-12-10T06:42:10.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Redis集群详细搭建指南">
<meta name="twitter:description" content="Redis 集群简介Redis 是一个开源的 key-value 存储系统，由于出众的性能，大部分互联网企业都用来做服务器端缓存。Redis 在3.0版本前只支持单实例模式，虽然支持主从模式、哨兵模式部署来解决单点故障，但是现在互联网企业动辄大几百G的数据，可完全是没法满足业务的需求，所以，Redis 在 3.0 版本以后就推出了集群模式。 Redis 从3.0.0正式版开始官方支持集群, Red">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://demo_demo.gitee.io/2017/06/04/redis-ji-qun-xiang-xi-da-jian-zhi-nan/">





  <title>Redis集群详细搭建指南 | 运维人</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>
<a href="https://github.com/guomaoqiu" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewbox="0 0 250 250" style="fill:#FD6C6C; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"/><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">运维人</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">生命在于折腾</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://demo_demo.gitee.io">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="OutMan">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/myphoto.png">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="运维人">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Redis集群详细搭建指南</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-06-04T14:17:26+08:00">
                2017-06-04
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="../../../../categories/数据库/" itemprop="url" rel="index">
                    <span itemprop="name">数据库</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="Redis-集群简介"><a href="#Redis-集群简介" class="headerlink" title="Redis 集群简介"></a>Redis 集群简介</h2><p>Redis 是一个开源的 key-value 存储系统，由于出众的性能，大部分互联网企业都用来做服务器端缓存。Redis 在3.0版本前只支持单实例模式，虽然支持主从模式、哨兵模式部署来解决单点故障，但是现在互联网企业动辄大几百G的数据，可完全是没法满足业务的需求，所以，Redis 在 3.0 版本以后就推出了集群模式。</p>
<p>Redis 从3.0.0正式版开始官方支持集群, Redis 集群采用了P2P的模式，完全去中心化。Redis 把所有的 Key 分成了 16384 个 slot，每个 Redis 实例负责其中一部分 slot 。集群中的所有信息（节点、端口、slot等），都通过节点之间定期的数据交换而更新。</p>
<p>Redis 客户端可以在任意一个 Redis 实例发出请求，如果所需数据不在该实例中，通过重定向命令引导客户端访问所需的实例。</p>
<h2 id="随随便便搭建一个集群"><a href="#随随便便搭建一个集群" class="headerlink" title="随随便便搭建一个集群"></a>随随便便搭建一个集群</h2><p>安装部署任何一个应用其实都很简单，只要安装步骤一步一步来就行了。下面说一下 Redis 集群搭建规划，由于集群至少需要6个节点（3主3从模式），所以，没有这么多机器给我玩，现在计划是在一台机器上模拟一个集群，当然，这和生产环境的集群搭建没本质区别。</p>
<h4 id="1-获取软件包"><a href="#1-获取软件包" class="headerlink" title="1.获取软件包"></a>1.获取软件包</h4><pre><code>cd /tmp/ &amp;&amp; wget http://download.redis.io/releases/redis-4.0.11.tar.gz
mkdir /usr/local/redis &amp;&amp; tar -xr /tmp/redis-4.0.11.tar.gz -C /usr/local/redis
cd /usr/local/redis
./configure
make -j 4
make install
</code></pre><h4 id="2-规划创建文件目录"><a href="#2-规划创建文件目录" class="headerlink" title="2.规划创建文件目录"></a>2.规划创建文件目录</h4><p>我们计划集群中 Redis 节点的端口号为 9001-9006 ，端口号即集群下各实例文件夹。数据存放在 端口号/data 文件夹中。</p>
<pre><code>mkdir /usr/local/redis-cluster/
cd /usr/local/redis-cluster/

for i in {1..6};do
    mkdir 900${i}/data
done
</code></pre><h4 id="3-复制执行脚本"><a href="#3-复制执行脚本" class="headerlink" title="3. 复制执行脚本"></a>3. 复制执行脚本</h4><p>在 /usr/local/redis-cluster 下创建 bin 文件夹，用来存放集群运行脚本，并把安装好的 Redis 的 src 路径下的运行脚本拷贝过来。看命令：</p>
<pre><code>mkdir /usr/local/redis-cluster/bin
cd /usr/local/redis/src # 下载后解压编译后的src目录
cp mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-server redis-trib.rb /usr/local/redis-cluster/bin
</code></pre><h4 id="4-复制一个新-Redis-实例"><a href="#4-复制一个新-Redis-实例" class="headerlink" title="4.复制一个新 Redis 实例"></a>4.复制一个新 Redis 实例</h4><p>我们现在从已安装好的 Redis 中复制一个新的实例到 9001 文件夹，并修改 redis.conf 配置。</p>
<pre><code>cp /usr/local/redis/* /usr/local/redis-cluster/9001


port 9001（每个节点的端口号）
daemonize yes
bind 192.168.56.127（绑定当前机器 IP）
dir /usr/local/redis-cluster/9001/data/（数据文件存放位置）
pidfile /var/run/redis_9001.pid（pid 9001和port要对应）
cluster-enabled yes（启动集群模式）
cluster-config-file nodes9001.conf（9001和port要对应）
cluster-node-timeout 15000
appendonly yes
</code></pre><h4 id="5-再复制出五个新-Redis-实例"><a href="#5-再复制出五个新-Redis-实例" class="headerlink" title="5.再复制出五个新 Redis 实例"></a>5.再复制出五个新 Redis 实例</h4><p>我们已经完成了一个节点了，其实接下来就是机械化的再完成另外五个节点，其实可以这么做：把 9001 实例 复制到另外五个文件夹中，唯一要修改的就是 redis.conf 中的所有和端口的相关的信息即可，其实就那么四个位置。开始操作，看命令：</p>
<pre><code>\cp -rf /usr/local/redis-cluster/9001/* /usr/local/redis-cluster/9002
\cp -rf /usr/local/redis-cluster/9001/* /usr/local/redis-cluster/9003
\cp -rf /usr/local/redis-cluster/9001/* /usr/local/redis-cluster/9004
\cp -rf /usr/local/redis-cluster/9001/* /usr/local/redis-cluster/9005
\cp -rf /usr/local/redis-cluster/9001/* /usr/local/redis-cluster/9006
</code></pre><p>这里我们把配置复制过去之后需要修改对应文件夹中的redis.conf 用sed或者vim全局替换即可，主要是端口</p>
<p>下面，下面我们来启动它们</p>
<pre><code>for i in {1..6};do
    /usr/local/bin/redis-server /usr/local/redis-cluster/900${i}/redis.conf 
done


[root@localhost redis-cluster]# ps aux | grep redis | grep -v grep
root       3995  0.5  0.6 151404 11864 ?        Ssl  10:51   0:10 /usr/local/redis-cluster/bin/redis-server 192.168.56.127:9001 [cluster]
root       4000  0.4  0.6 151404 11856 ?        Ssl  10:51   0:09 /usr/local/redis-cluster/bin/redis-server 192.168.56.127:9002 [cluster]
root       4005  0.4  0.6 151404 11856 ?        Ssl  10:51   0:09 /usr/local/redis-cluster/bin/redis-server 192.168.56.127:9003 [cluster]
root       4010  0.1  0.6 151404 11872 ?        Ssl  10:51   0:02 /usr/local/redis-cluster/bin/redis-server 192.168.56.127:9004 [cluster]
root       4015  0.0  0.6 151404 11876 ?        Ssl  10:51   0:01 /usr/local/redis-cluster/bin/redis-server 192.168.56.127:9005 [cluster]
root       4020  0.0  0.6 151404 11872 ?        Ssl  10:51   0:01 /usr/local/redis-cluster/bin/redis-server 192.168.56.127:9006 [cluster]
root       4509  0.8  0.6 149356 11880 ?        Ssl  10:55   0:14 /usr/local/redis-cluster/bin/redis-server 
[root@localhost redis-cluster]#
</code></pre><p>以上 可以看出我们的6个实例已经启动了<br>测试一下？</p>
<p>随便找个节点测试一下:</p>
<pre><code>/usr/local/redis-cluster/bin/redis-cli -h 192.168.56.127 -p 9001

192.168.56.127:9001&gt; set name guomaoqiu
(error) CLUSTERDOWN Hash slot not served
192.168.56.127:9001&gt;
</code></pre><p>连接成功了，但好像报错了</p>
<p>这是因为虽然我们配置并启动了 Redis 集群服务，但是他们暂时还并不在一个集群中，互相直接发现不了，而且还没有可存储的位置，就是所谓的slot（槽）。</p>
<h4 id="6-安装集群所需软件"><a href="#6-安装集群所需软件" class="headerlink" title="6.安装集群所需软件"></a>6.安装集群所需软件</h4><p>由于 Redis 集群需要使用 ruby 命令，所以我们需要安装 ruby 和相关接口。</p>
<pre><code>安装redis需要ruby版本最低是2.2.2，而centos yum库中ruby版本支持到2.0.0。所以，无法满足需求。
解决方案：

1.安装curl
sudo yum install curl
2.安装RVM
curl -L get.rvm.io | bash -s stable
source /usr/local/rvm/scripts/rvm
3.查看rvm库中已知的ruby版本
rvm list known
4.安装一个ruby版本
rvm install 2.3.3
5.设置默认版本
rvm use 2.3.3
6.卸载一个已知版本
rvm remove 2.0.0
7.查看ruby版本
ruby –version
8.再安装redis就可以了
gem install redis
</code></pre><h4 id="7-利用官方提供的命令创建集群"><a href="#7-利用官方提供的命令创建集群" class="headerlink" title="7. 利用官方提供的命令创建集群"></a>7. 利用官方提供的命令创建集群</h4><pre><code>/usr/local/redis-cluster/bin/redis-trib.rb create --replicas 1 192.168.56.127:9001 192.168.56.127:9002 192.168.56.127:9003 192.168.56.127:9004 192.168.56.127:9005 192.168.56.127:9006
</code></pre><p>简单解释一下这个命令：调用 ruby 命令来进行创建集群，–replicas 1 表示主从复制比例为 1:1，即一个主节点对应一个从节点；然后，默认给我们分配好了每个主节点和对应从节点服务，以及 solt 的大小，因为在 Redis 集群中有且仅有 16383 个 solt ，默认情况会给我们平均分配，当然你可以指定，后续的增减节点也可以重新分配。</p>
<pre><code>[root@localhost redis-cluster]# /usr/local/redis-cluster/bin/redis-trib.rb create --replicas 1 192.168.56.127:9001 192.168.56.127:9002 192.168.56.127:9003 192.168.56.127:9004 192.168.56.127:9005 192.168.56.127:9006
&gt;&gt;&gt; Creating cluster
&gt;&gt;&gt; Performing hash slots allocation on 6 nodes...
Using 3 masters:
# 三个主节点
192.168.56.127:9001
192.168.56.127:9002
192.168.56.127:9003
# 三个主节点对应三个从节点
Adding replica 192.168.56.127:9005 to 192.168.56.127:9001
Adding replica 192.168.56.127:9006 to 192.168.56.127:9002
Adding replica 192.168.56.127:9004 to 192.168.56.127:9003
&gt;&gt;&gt; Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 0fcbaa301451baf87284546513003568e47b5daa 192.168.56.127:9001
   slots:0-5460 (5461 slots) master
M: 474fd53f7199ad70cce7f18bd68f5445b559509a 192.168.56.127:9002
   slots:5461-10922 (5462 slots) master
M: e070e76789352d2492cfc9800850e943e0c4b72d 192.168.56.127:9003
   slots:10923-16383 (5461 slots) master
S: 950c399165439660efb6ff2f907f938e213de530 192.168.56.127:9004
   replicates 474fd53f7199ad70cce7f18bd68f5445b559509a
S: 17db098db9fd222df002e649506f88efcdc83633 192.168.56.127:9005
   replicates e070e76789352d2492cfc9800850e943e0c4b72d
S: bd278fcea63862199e964a6aab2e786710cf5575 192.168.56.127:9006
   replicates 0fcbaa301451baf87284546513003568e47b5daa
Can I set the above configuration? (type &apos;yes&apos; to accept): yes
&gt;&gt;&gt; Nodes configuration updated
&gt;&gt;&gt; Assign a different config epoch to each node
&gt;&gt;&gt; Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join...
&gt;&gt;&gt; Performing Cluster Check (using node 192.168.56.127:9001)
M: 0fcbaa301451baf87284546513003568e47b5daa 192.168.56.127:9001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 474fd53f7199ad70cce7f18bd68f5445b559509a 192.168.56.127:9002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: e070e76789352d2492cfc9800850e943e0c4b72d 192.168.56.127:9003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: bd278fcea63862199e964a6aab2e786710cf5575 192.168.56.127:9006
   slots: (0 slots) slave
   replicates 0fcbaa301451baf87284546513003568e47b5daa
S: 950c399165439660efb6ff2f907f938e213de530 192.168.56.127:9004
   slots: (0 slots) slave
   replicates 474fd53f7199ad70cce7f18bd68f5445b559509a
S: 17db098db9fd222df002e649506f88efcdc83633 192.168.56.127:9005
   slots: (0 slots) slave
   replicates e070e76789352d2492cfc9800850e943e0c4b72d
[OK] All nodes agree about slots configuration.
&gt;&gt;&gt; Check for open slots...
&gt;&gt;&gt; Check slots coverage...
[OK] All 16384 slots covered.
</code></pre><p>以上执行结果代表集群搭建成功啦！！！</p>
<p>验证：</p>
<pre><code>/usr/local/redis-cluster/bin/redis-cli -c -h 192.168.56.127 -p 9001
192.168.56.127:9001&gt; cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:167
cluster_stats_messages_pong_sent:172
cluster_stats_messages_sent:339
cluster_stats_messages_ping_received:167
cluster_stats_messages_pong_received:167
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:339
192.168.56.127:9001&gt; cluster nodes
474fd53f7199ad70cce7f18bd68f5445b559509a 192.168.56.127:9002@19002 master - 0 1536205294229 2 connected 5461-10922
e070e76789352d2492cfc9800850e943e0c4b72d 192.168.56.127:9003@19003 master - 0 1536205293000 3 connected 10923-16383
bd278fcea63862199e964a6aab2e786710cf5575 192.168.56.127:9006@19006 slave 0fcbaa301451baf87284546513003568e47b5daa 0 1536205295234 6 connected
0fcbaa301451baf87284546513003568e47b5daa 192.168.56.127:9001@19001 myself,master - 0 1536205294000 1 connected 0-5460
950c399165439660efb6ff2f907f938e213de530 192.168.56.127:9004@19004 slave 474fd53f7199ad70cce7f18bd68f5445b559509a 0 1536205293217 4 connected
17db098db9fd222df002e649506f88efcdc83633 192.168.56.127:9005@19005 slave e070e76789352d2492cfc9800850e943e0c4b72d 0 1536205292212 5 connected
192.168.56.127:9001&gt; set name guomaoqiu
-&gt; Redirected to slot [5798] located at 192.168.56.127:9002
OK
192.168.56.127:9002&gt; get name
&quot;guomaoqiu&quot;
192.168.56.127:9002&gt;
</code></pre><p>通过命令，可以详细的看出集群信息和各个节点状态，主从信息以及连接数、槽信息等。这么看到，我们已经真的把 Redis 集群搭建部署成功啦！</p>
<h2 id="如何新增节点？"><a href="#如何新增节点？" class="headerlink" title="如何新增节点？"></a>如何新增节点？</h2><p>新增节点无非就是复制配置，修改配置，然后启动；我这里还是创建了一对一主一从的配置，端口分别对应的是9007、9008</p>
<h4 id="1-启动两个实例-假设我这里已经复制配置，修改完配置了"><a href="#1-启动两个实例-假设我这里已经复制配置，修改完配置了" class="headerlink" title="1.启动两个实例(假设我这里已经复制配置，修改完配置了)"></a>1.启动两个实例(假设我这里已经复制配置，修改完配置了)</h4><pre><code>for i in {7..8};do
    /usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/900${i}/redis.conf 
done

6002:C 06 Sep 11:50:53.048 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6002:C 06 Sep 11:50:53.048 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=6002, just started
6002:C 06 Sep 11:50:53.048 # Configuration loaded
6004:C 06 Sep 11:50:53.060 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6004:C 06 Sep 11:50:53.060 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=6004, just started
6004:C 06 Sep 11:50:53.060 # Configuration loaded

ps aux | grep redis | grep -v grep
redis      1180  0.0  0.3 142900  5828 ?        Ssl  11:28   0:01 /usr/bin/redis-server 0.0.0.0:6379
root       4257  0.0  0.6 149356 11880 ?        Ssl  11:32   0:00 /usr/local/redis-cluster/bin/redis-server 192.168.56.127:9001 [cluster]
root       4262  0.0  0.6 149356 11880 ?        Ssl  11:32   0:00 /usr/local/redis-cluster/bin/redis-server 192.168.56.127:9002 [cluster]
root       4267  0.0  0.6 149356 11876 ?        Ssl  11:32   0:00 /usr/local/redis-cluster/bin/redis-server 192.168.56.127:9003 [cluster]
root       4272  0.0  0.6 149356 11908 ?        Ssl  11:32   0:00 /usr/local/redis-cluster/bin/redis-server 192.168.56.127:9004 [cluster]
root       4277  0.0  0.6 149356 11920 ?        Ssl  11:32   0:00 /usr/local/redis-cluster/bin/redis-server 192.168.56.127:9005 [cluster]
root       4282  0.0  0.6 149356 11912 ?        Ssl  11:32   0:00 /usr/local/redis-cluster/bin/redis-server 192.168.56.127:9006 [cluster]
root       6003  0.1  0.5 147308  9616 ?        Ssl  11:50   0:00 /usr/local/redis-cluster/bin/redis-server 192.168.56.127:9007 [cluster]
root       6008  0.0  0.5 147308  9616 ?        Ssl  11:50   0:00 /usr/local/redis-cluster/bin/redis-server 192.168.56.127:9008 [cluster]
</code></pre><p>以上 可以看到我们启动了后面两个实例，并且进程中标注了是以集群方式启动, 但是此时还并没有将实例加入到集群当中。</p>
<h4 id="2-添加实例到集群"><a href="#2-添加实例到集群" class="headerlink" title="2.添加实例到集群"></a>2.添加实例到集群</h4><pre><code>[root@locahost redis-cluster]# /usr/local/redis-cluster/bin/redis-trib.rb add-node 192.168.56.127:9007 192.168.56.127:9001
&gt;&gt;&gt; Adding node 192.168.56.127:9007 to cluster 192.168.56.127:9001
&gt;&gt;&gt; Performing Cluster Check (using node 192.168.56.127:9001)
M: 0fcbaa301451baf87284546513003568e47b5daa 192.168.56.127:9001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 474fd53f7199ad70cce7f18bd68f5445b559509a 192.168.56.127:9002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: e070e76789352d2492cfc9800850e943e0c4b72d 192.168.56.127:9003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: bd278fcea63862199e964a6aab2e786710cf5575 192.168.56.127:9006
   slots: (0 slots) slave
   replicates 0fcbaa301451baf87284546513003568e47b5daa
S: 950c399165439660efb6ff2f907f938e213de530 192.168.56.127:9004
   slots: (0 slots) slave
   replicates 474fd53f7199ad70cce7f18bd68f5445b559509a
S: 17db098db9fd222df002e649506f88efcdc83633 192.168.56.127:9005
   slots: (0 slots) slave
   replicates e070e76789352d2492cfc9800850e943e0c4b72d
[OK] All nodes agree about slots configuration.
&gt;&gt;&gt; Check for open slots...
&gt;&gt;&gt; Check slots coverage...
[OK] All 16384 slots covered.
&gt;&gt;&gt; Send CLUSTER MEET to node 192.168.56.127:9007 to make it join the cluster.
[OK] New node added correctly.
[root@localhost redis-cluster]# /usr/local/redis-cluster/bin/redis-cli -c -h 192.168.56.127 -p 9001 cluster nodes
474fd53f7199ad70cce7f18bd68f5445b559509a 192.168.56.127:9002@19002 master - 0 1536215194000 2 connected 5461-10922
e070e76789352d2492cfc9800850e943e0c4b72d 192.168.56.127:9003@19003 master - 0 1536215194000 3 connected 10923-16383
bd278fcea63862199e964a6aab2e786710cf5575 192.168.56.127:9006@19006 slave 0fcbaa301451baf87284546513003568e47b5daa 0 1536215193000 6 connected
0fcbaa301451baf87284546513003568e47b5daa 192.168.56.127:9001@19001 myself,master - 0 1536215192000 1 connected 0-5460
950c399165439660efb6ff2f907f938e213de530 192.168.56.127:9004@19004 slave 474fd53f7199ad70cce7f18bd68f5445b559509a 0 1536215195858 4 connected
1714784542b3afc9e2a8b2c93fff49d095e7d72b 192.168.56.127:9007@19007 master - 0 1536215195000 0 connected
17db098db9fd222df002e649506f88efcdc83633 192.168.56.127:9005@19005 slave e070e76789352d2492cfc9800850e943e0c4b72d 0 1536215194855 5 connected
</code></pre><p>以上可以看到我们通过 add-node 参数添加了 9007这个实例；命令最后一个参数是已经成为集群的集群成员host:port<br>而且实例9007获得了一个集群ID:<code>1714784542b3afc9e2a8b2c93fff49d095e7d72b</code></p>
<h4 id="5-增加实例的从节点到集群"><a href="#5-增加实例的从节点到集群" class="headerlink" title="5. 增加实例的从节点到集群"></a>5. 增加实例的从节点到集群</h4><p>以上我们将实例9007作为master增加到了集群中，但是按照前面的规划，每个master对应一个slave所以我们需要将实例9008加入集群并将其称为9007的slave,命令如下</p>
<pre><code>[root@localhost redis-cluster]# /usr/local/redis-cluster/bin/redis-trib.rb add-node --slave --master-id 1714784542b3afc9e2a8b2c93fff49d095e7d72b 192.168.56.127:9008 192.168.56.127:9001
&gt;&gt;&gt; Adding node 192.168.56.127:9008 to cluster 192.168.56.127:9001
&gt;&gt;&gt; Performing Cluster Check (using node 192.168.56.127:9001)
M: 0fcbaa301451baf87284546513003568e47b5daa 192.168.56.127:9001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 474fd53f7199ad70cce7f18bd68f5445b559509a 192.168.56.127:9002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: e070e76789352d2492cfc9800850e943e0c4b72d 192.168.56.127:9003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: bd278fcea63862199e964a6aab2e786710cf5575 192.168.56.127:9006
   slots: (0 slots) slave
   replicates 0fcbaa301451baf87284546513003568e47b5daa
S: 950c399165439660efb6ff2f907f938e213de530 192.168.56.127:9004
   slots: (0 slots) slave
   replicates 474fd53f7199ad70cce7f18bd68f5445b559509a
M: 1714784542b3afc9e2a8b2c93fff49d095e7d72b 192.168.56.127:9007
   slots: (0 slots) master
   0 additional replica(s)
S: 17db098db9fd222df002e649506f88efcdc83633 192.168.56.127:9005
   slots: (0 slots) slave
   replicates e070e76789352d2492cfc9800850e943e0c4b72d
[OK] All nodes agree about slots configuration.
&gt;&gt;&gt; Check for open slots...
&gt;&gt;&gt; Check slots coverage...
[OK] All 16384 slots covered.
&gt;&gt;&gt; Send CLUSTER MEET to node 192.168.56.127:9008 to make it join the cluster.
Waiting for the cluster to join.
&gt;&gt;&gt; Configure node as replica of 192.168.56.127:9007.
[OK] New node added correctly.

[root@localhost redis-cluster]# /usr/local/redis-cluster/bin/redis-trib.rb check 192.168.56.127:9001
&gt;&gt;&gt; Performing Cluster Check (using node 192.168.56.127:9001)
M: 0fcbaa301451baf87284546513003568e47b5daa 192.168.56.127:9001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 474fd53f7199ad70cce7f18bd68f5445b559509a 192.168.56.127:9002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: e070e76789352d2492cfc9800850e943e0c4b72d 192.168.56.127:9003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 5ed978b695e6a222e67d13c68e55f6c4e74b7ba6 192.168.56.127:9008
   slots: (0 slots) slave
   replicates 1714784542b3afc9e2a8b2c93fff49d095e7d72b
S: bd278fcea63862199e964a6aab2e786710cf5575 192.168.56.127:9006
   slots: (0 slots) slave
   replicates 0fcbaa301451baf87284546513003568e47b5daa
S: 950c399165439660efb6ff2f907f938e213de530 192.168.56.127:9004
   slots: (0 slots) slave
   replicates 474fd53f7199ad70cce7f18bd68f5445b559509a
M: 1714784542b3afc9e2a8b2c93fff49d095e7d72b 192.168.56.127:9007
   slots: (0 slots) master
   1 additional replica(s)
S: 17db098db9fd222df002e649506f88efcdc83633 192.168.56.127:9005
   slots: (0 slots) slave
   replicates e070e76789352d2492cfc9800850e943e0c4b72d
[OK] All nodes agree about slots configuration.
&gt;&gt;&gt; Check for open slots...
&gt;&gt;&gt; Check slots coverage...
[OK] All 16384 slots covered.
</code></pre><p>以上我们将两个节点加入到了集群中，但是细心的同学会发现我们的实例9007所拥有的slot数量为0，那此时就需要将重新调整哈希槽啦~<br>再次之前我们三个master 平均将16384 分成了三等分，那我们新增加了一个master ，为了平衡我们可以计算出这个新的master需要多少哈希槽，即 16384/4 = 4096 我们需要移动4096个槽点到9007上。</p>
<pre><code>[root@localhost redis-cluster]# /usr/local/redis-cluster/bin/redis-trib.rb reshard 192.168.56.127:9001
&gt;&gt;&gt; Performing Cluster Check (using node 192.168.56.127:9001)
M: 0fcbaa301451baf87284546513003568e47b5daa 192.168.56.127:9001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 5ed978b695e6a222e67d13c68e55f6c4e74b7ba6 192.168.56.127:9008
   slots: (0 slots) slave
   replicates 1714784542b3afc9e2a8b2c93fff49d095e7d72b
M: 1714784542b3afc9e2a8b2c93fff49d095e7d72b 192.168.56.127:9007
   slots:5461-5798 (0 slots) master
   1 additional replica(s)
S: 17db098db9fd222df002e649506f88efcdc83633 192.168.56.127:9005
   slots: (0 slots) slave
   replicates e070e76789352d2492cfc9800850e943e0c4b72d
M: 474fd53f7199ad70cce7f18bd68f5445b559509a 192.168.56.127:9002
   slots:5799-10922 (5462 slots) master
   1 additional replica(s)
M: e070e76789352d2492cfc9800850e943e0c4b72d 192.168.56.127:9003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: bd278fcea63862199e964a6aab2e786710cf5575 192.168.56.127:9006
   slots: (0 slots) slave
   replicates 0fcbaa301451baf87284546513003568e47b5daa
S: 950c399165439660efb6ff2f907f938e213de530 192.168.56.127:9004
   slots: (0 slots) slave
   replicates 474fd53f7199ad70cce7f18bd68f5445b559509a
[OK] All nodes agree about slots configuration.
&gt;&gt;&gt; Check for open slots...
&gt;&gt;&gt; Check slots coverage...
[OK] All 16384 slots covered.
# 这里需要输入我们所需要的哈希槽
How many slots do you want to move (from 1 to 16384)? 4097
# 这里需要输入我们实例9007的ID
What is the receiving node ID? 1714784542b3afc9e2a8b2c93fff49d095e7d72b
Please enter all the source node IDs.
  Type &apos;all&apos; to use all the nodes as source nodes for the hash slots.
  Type &apos;done&apos; once you entered all the source nodes IDs.
Source node #1:all
#输入all 表示从所有的主节点中随机转移，凑够1000个哈希槽
#然后再输入yes，redis集群就开始分配哈希槽了。
</code></pre><p>以上:<br>redis-trib 会向你询问重新分片的源节点（source node），即，要从特点的哪个节点中取出 4096 个哈希槽，还是从全部节点提取4096个哈希槽， 并将这些槽移动到9007节点上面。</p>
<p>如果我们不打算从特定的节点上取出指定数量的哈希槽，那么可以向redis-trib输入 all，这样的话， 集群中的所有主节点都会成为源节点，redis-trib从各个源节点中各取出一部分哈希槽，凑够4096个，然后移动到9007节点上：</p>
<p>验证：</p>
<pre><code>[root@localhost redis-cluster]# /usr/local/redis-cluster/bin/redis-trib.rb check 192.168.56.127:9001
&gt;&gt;&gt; Performing Cluster Check (using node 192.168.56.127:9001)
M: 0fcbaa301451baf87284546513003568e47b5daa 192.168.56.127:9001
   slots:1280-5460 (4096 slots) master
   1 additional replica(s)
S: 5ed978b695e6a222e67d13c68e55f6c4e74b7ba6 192.168.56.127:9008
   slots: (0 slots) slave
   replicates 1714784542b3afc9e2a8b2c93fff49d095e7d72b
M: 1714784542b3afc9e2a8b2c93fff49d095e7d72b 192.168.56.127:9007
   slots:0-1279,5461-6998,10923-12201 (4096 slots) master
   1 additional replica(s)
S: 17db098db9fd222df002e649506f88efcdc83633 192.168.56.127:9005
   slots: (0 slots) slave
   replicates e070e76789352d2492cfc9800850e943e0c4b72d
M: 474fd53f7199ad70cce7f18bd68f5445b559509a 192.168.56.127:9002
   slots:6999-10922 (4096 slots) master
   1 additional replica(s)
M: e070e76789352d2492cfc9800850e943e0c4b72d 192.168.56.127:9003
   slots:12202-16383 (4096 slots) master
   1 additional replica(s)
S: bd278fcea63862199e964a6aab2e786710cf5575 192.168.56.127:9006
   slots: (0 slots) slave
   replicates 0fcbaa301451baf87284546513003568e47b5daa
S: 950c399165439660efb6ff2f907f938e213de530 192.168.56.127:9004
   slots: (0 slots) slave
   replicates 474fd53f7199ad70cce7f18bd68f5445b559509a
[OK] All nodes agree about slots configuration.
&gt;&gt;&gt; Check for open slots...
&gt;&gt;&gt; Check slots coverage...
[OK] All 16384 slots covered.
</code></pre><p>以上我们的集群搭建以及新增节点就完成了；</p>
<h4 id="6-从集群中删除节点"><a href="#6-从集群中删除节点" class="headerlink" title="6. 从集群中删除节点:"></a>6. 从集群中删除节点:</h4><p>和节点添加一样，移除节点也有移除主节点，从节点</p>
<h5 id="1、移除主节点"><a href="#1、移除主节点" class="headerlink" title="1、移除主节点"></a>1、移除主节点</h5><p>移除节点使用redis-trib的del-node命令</p>
<pre><code>/usr/local/redis-cluster/bin/redis-trib.rb del-node 192.168.56.127:9001 ${node-id}
</code></pre><p>192.168.56.127:9001其中一个集群节点，node-id为要删除的主节点,这里和添加节点不同,移除节点node-id是必需的，测试删除9003主节点</p>
<pre><code>[root@localhost redis-cluster]# /usr/local/redis-cluster/bin/redis-trib.rb del-node 192.168.56.127:9001 e070e76789352d2492cfc9800850e943e0c4b72d
&gt;&gt;&gt; Removing node e070e76789352d2492cfc9800850e943e0c4b72d from cluster 192.168.56.127:9001
[ERR] Node 192.168.56.127:9003 is not empty! Reshard data away and try again.
</code></pre><p>redis cluster提示9003已经有数据了，不能够被删除，需要将他的数据转移出去，也就是和新增主节点一样需重新分片。</p>
<pre><code>/usr/local/redis-cluster/bin/redis-trib.rb reshard 192.168.56.127:9001
</code></pre><p>执行以后会提示我们移除的大小，因为9003占用了4096个槽点</p>
<pre><code>&gt;&gt;&gt; Check for open slots...
&gt;&gt;&gt; Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)?
</code></pre><p>输入4096<br>提示移动的node id，填写9007的node id。</p>
<pre><code>&gt;&gt;&gt; Check for open slots...
&gt;&gt;&gt; Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 4182
What is the receiving node ID? 1714784542b3afc9e2a8b2c93fff49d095e7d72b
</code></pre><p>需要移动到全部主节点上还是单个主节点</p>
<pre><code>Please enter all the source node IDs.
  Type &apos;all&apos; to use all the nodes as source nodes for the hash slots.
  Type &apos;done&apos; once you entered all the source nodes IDs.
Source node #1:
</code></pre><p>将4096个槽点移动到9007上，填写9003的node id ：e070e76789352d2492cfc9800850e943e0c4b72d</p>
<pre><code>Source node #1:e070e76789352d2492cfc9800850e943e0c4b72d
Source node #2:done
</code></pre><p>确认之后会一个一个将9003的卡槽移到到9007上。</p>
<pre><code>[root@localhost redis-cluster]# /usr/local/redis-cluster/bin/redis-trib.rb check 192.168.56.127:9001
&gt;&gt;&gt; Performing Cluster Check (using node 192.168.56.127:9001)
M: 0fcbaa301451baf87284546513003568e47b5daa 192.168.56.127:9001
   slots:1280-5460 (4096 slots) master
   1 additional replica(s)
S: 5ed978b695e6a222e67d13c68e55f6c4e74b7ba6 192.168.56.127:9008
   slots: (0 slots) slave
   replicates 1714784542b3afc9e2a8b2c93fff49d095e7d72b
M: 1714784542b3afc9e2a8b2c93fff49d095e7d72b 192.168.56.127:9007
   slots:0-1279,5461-6998,10923-16383 (8192 slots) master
   2 additional replica(s)
S: 17db098db9fd222df002e649506f88efcdc83633 192.168.56.127:9005
   slots: (0 slots) slave
   replicates 1714784542b3afc9e2a8b2c93fff49d095e7d72b
M: 474fd53f7199ad70cce7f18bd68f5445b559509a 192.168.56.127:9002
   slots:6999-10922 (4096 slots) master
   1 additional replica(s)
M: e070e76789352d2492cfc9800850e943e0c4b72d 192.168.56.127:9003
   slots: (0 slots) master
   0 additional replica(s)
S: bd278fcea63862199e964a6aab2e786710cf5575 192.168.56.127:9006
   slots: (0 slots) slave
   replicates 0fcbaa301451baf87284546513003568e47b5daa
S: 950c399165439660efb6ff2f907f938e213de530 192.168.56.127:9004
   slots: (0 slots) slave
   replicates 474fd53f7199ad70cce7f18bd68f5445b559509a
[OK] All nodes agree about slots configuration.
&gt;&gt;&gt; Check for open slots...
&gt;&gt;&gt; Check slots coverage...
[OK] All 16384 slots covered.
</code></pre><p>以上我们将实例9003的哈希槽全部转移到了9007，已经为0 ,此时再来删除我们的节点就行了~</p>
<pre><code>[root@localhost redis-cluster]# /usr/local/redis-cluster/bin/redis-trib.rb del-node 192.168.56.127:9001 e070e76789352d2492cfc9800850e943e0c4b72d
&gt;&gt;&gt; Removing node e070e76789352d2492cfc9800850e943e0c4b72d from cluster 192.168.56.127:9001
&gt;&gt;&gt; Sending CLUSTER FORGET messages to the cluster...
&gt;&gt;&gt; SHUTDOWN the node.

# 看下面结果 执行完del-node 9003节点就已经从集群中剔除啦~😁
&gt;&gt;&gt; Performing Cluster Check (using node 192.168.56.127:9001)
M: 0fcbaa301451baf87284546513003568e47b5daa 192.168.56.127:9001
   slots:1280-5460 (4096 slots) master
   1 additional replica(s)
S: 5ed978b695e6a222e67d13c68e55f6c4e74b7ba6 192.168.56.127:9008
   slots: (0 slots) slave
   replicates 1714784542b3afc9e2a8b2c93fff49d095e7d72b
M: 1714784542b3afc9e2a8b2c93fff49d095e7d72b 192.168.56.127:9007
   slots:0-1279,5461-6998,10923-16383 (8192 slots) master
   2 additional replica(s)
S: 17db098db9fd222df002e649506f88efcdc83633 192.168.56.127:9005
   slots: (0 slots) slave
   replicates 1714784542b3afc9e2a8b2c93fff49d095e7d72b
M: 474fd53f7199ad70cce7f18bd68f5445b559509a 192.168.56.127:9002
   slots:6999-10922 (4096 slots) master
   1 additional replica(s)
S: bd278fcea63862199e964a6aab2e786710cf5575 192.168.56.127:9006
   slots: (0 slots) slave
   replicates 0fcbaa301451baf87284546513003568e47b5daa
S: 950c399165439660efb6ff2f907f938e213de530 192.168.56.127:9004
   slots: (0 slots) slave
   replicates 474fd53f7199ad70cce7f18bd68f5445b559509a
[OK] All nodes agree about slots configuration.
&gt;&gt;&gt; Check for open slots...
&gt;&gt;&gt; Check slots coverage...
[OK] All 16384 slots covered.
</code></pre><h2 id="最后"><a href="#最后" class="headerlink" title="最后"></a>最后</h2><p>当然我们集群搭建已经完成，那集群的作用是为了解决分片的问题，如果需要搭建高可用集群；也就是主的失败后从的能够切换角色,<br>所以可以使用哨兵机制来解决HA.<br>sentinel和cluster主要区别，sentinel用来解决HA（高可用）问题，而cluster主要解决sharding（分片）问题。两个经常结合使用搭建高可用集群。</p>
<h2 id="关于哈希槽的概念："><a href="#关于哈希槽的概念：" class="headerlink" title="关于哈希槽的概念："></a>关于哈希槽的概念：</h2><p>Redis 集群中内置了 16384 个哈希槽，当需要在 Redis 集群中放置一个 key-value时，redis 先对 key 使用 crc16 算法算出一个结果，然后把结果对 16384 求余数，这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽，redis 会根据节点数量大致均等的将哈希槽映射到不同的节点。</p>
<p>Redis 集群没有使用一致性hash, 而是引入了哈希槽的概念。</p>
<p>Redis 集群有16384个哈希槽,每个key通过CRC16校验后对16384取模来决定放置哪个槽.集群的每个节点负责一部分hash槽。这种结构很容易添加或者删除节点，并且无论是添加删除或者修改某一个节点，都不会造成集群不可用的状态。</p>
<p>使用哈希槽的好处就在于可以方便的添加或移除节点。</p>
<p>当需要增加节点时，只需要把其他节点的某些哈希槽挪到新节点就可以了；</p>
<p>当需要移除节点时，只需要把移除节点上的哈希槽挪到其他节点就行了；</p>
<p>在这一点上，我们以后新增或移除节点的时候不用先停掉所有的 redis 服务。</p>
<p>“用了哈希槽的概念，而没有用一致性哈希算法，不都是哈希么？这样做的原因是为什么呢？”<br>Redis Cluster是自己做的crc16的简单hash算法，没有用一致性hash。Redis的作者认为它的crc16(key) mod 16384的效果已经不错了，虽然没有一致性hash灵活，但实现很简单，节点增删时处理起来也很方便。</p>
<p>“为了动态增删节点的时候，不至于丢失数据么？”<br>节点增删时不丢失数据和hash算法没什么关系，不丢失数据要求的是一份数据有多个副本。</p>
<p>“还有集群总共有2的14次方，16384个哈希槽，那么每一个哈希槽中存的key 和 value是什么？”<br>当你往Redis Cluster中加入一个Key时，会根据crc16(key) mod 16384计算这个key应该分布到哪个hash slot中，一个hash slot中会有很多key和value。你可以理解成表的分区，使用单节点时的redis时只有一个表，所有的key都放在这个表里；改用Redis Cluster以后会自动为你生成16384个分区表，你insert数据时会根据上面的简单算法来决定你的key应该存在哪个分区，每个分区里有很多key。</p>

      
    </div>
    
    
    
<div>
  
    <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>


  
</div>
    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="../../../../tags/redis-cluster/" rel="tag"># redis-cluster</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="../../../05/31/lan-jing-ping-tai-shi-jian-ji-ying-yong-2/" rel="next" title="蓝鲸平台实践及应用...">
                <i class="fa fa-chevron-left"></i> 蓝鲸平台实践及应用...
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="../../../11/24/ru-he-shi-yongcelery-qin-cai-yi-bu-shen-qi-zhi-xin/" rel="prev" title="如何使用Celery(芹菜)异步神器执行后台任务">
                如何使用Celery(芹菜)异步神器执行后台任务 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/myphoto.png" alt="OutMan">
            
              <p class="site-author-name" itemprop="name">OutMan</p>
              <p class="site-description motion-element" itemprop="description">😊</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="../../../../archives/">
              
                  <span class="site-state-item-count">79</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="../../../../categories/index.html">
                  <span class="site-state-item-count">22</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="../../../../tags/index.html">
                  <span class="site-state-item-count">46</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/guomaoqiu" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:guomaoqiu@google.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i></a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Redis-集群简介"><span class="nav-number">1.</span> <span class="nav-text">Redis 集群简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#随随便便搭建一个集群"><span class="nav-number">2.</span> <span class="nav-text">随随便便搭建一个集群</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-获取软件包"><span class="nav-number">2.0.1.</span> <span class="nav-text">1.获取软件包</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-规划创建文件目录"><span class="nav-number">2.0.2.</span> <span class="nav-text">2.规划创建文件目录</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-复制执行脚本"><span class="nav-number">2.0.3.</span> <span class="nav-text">3. 复制执行脚本</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-复制一个新-Redis-实例"><span class="nav-number">2.0.4.</span> <span class="nav-text">4.复制一个新 Redis 实例</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-再复制出五个新-Redis-实例"><span class="nav-number">2.0.5.</span> <span class="nav-text">5.再复制出五个新 Redis 实例</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-安装集群所需软件"><span class="nav-number">2.0.6.</span> <span class="nav-text">6.安装集群所需软件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#7-利用官方提供的命令创建集群"><span class="nav-number">2.0.7.</span> <span class="nav-text">7. 利用官方提供的命令创建集群</span></a></li></ol></li></ol><li class="nav-item nav-level-2"><a class="nav-link" href="#如何新增节点？"><span class="nav-number">3.</span> <span class="nav-text">如何新增节点？</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-启动两个实例-假设我这里已经复制配置，修改完配置了"><span class="nav-number">3.0.1.</span> <span class="nav-text">1.启动两个实例(假设我这里已经复制配置，修改完配置了)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-添加实例到集群"><span class="nav-number">3.0.2.</span> <span class="nav-text">2.添加实例到集群</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-增加实例的从节点到集群"><span class="nav-number">3.0.3.</span> <span class="nav-text">5. 增加实例的从节点到集群</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-从集群中删除节点"><span class="nav-number">3.0.4.</span> <span class="nav-text">6. 从集群中删除节点:</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#1、移除主节点"><span class="nav-number">3.0.4.1.</span> <span class="nav-text">1、移除主节点</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#最后"><span class="nav-number">4.</span> <span class="nav-text">最后</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#关于哈希槽的概念："><span class="nav-number">5.</span> <span class="nav-text">关于哈希槽的概念：</span></a></li></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">OutMan</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="../../../../lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="../../../../lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="../../../../lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="../../../../lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="../../../../lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="../../../../lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="../../../../lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="../../../../js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="../../../../js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="../../../../js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="../../../../js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="../../../../js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="../../../../js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="../../../../js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
